package com.elder.business.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.elder.business.domain.ElderlyInfo;
import com.elder.business.domain.Elderlyapartment;
import com.elder.business.mapper.ElderlyInfoMapper;
import com.elder.business.mapper.ElderlyapartmentMapper;
import com.elder.common.core.page.TableDataInfo;
import com.elder.common.utils.DateUtils;
import com.elder.common.utils.StringNewUtils;
import com.elder.common.utils.uuid.SnowGenerator;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.elder.business.mapper.ElderlyApartmentVisitRecordsMapper;
import com.elder.business.domain.ElderlyApartmentVisitRecords;
import com.elder.business.service.IElderlyApartmentVisitRecordsService;
import org.springframework.util.CollectionUtils;

/**
 * 探访记录Service业务层处理
 * 
 * @author zyk
 * @date 2023-11-26
 */
@Service
public class ElderlyApartmentVisitRecordsServiceImpl implements IElderlyApartmentVisitRecordsService 
{
    @Autowired
    private ElderlyApartmentVisitRecordsMapper elderlyApartmentVisitRecordsMapper;
    @Autowired
    private ElderlyapartmentMapper elderlyapartmentMapper;
    @Autowired
    private ElderlyInfoMapper elderlyInfoMapper;
    /**
     * 查询探访记录
     * 
     * @param id 探访记录主键
     * @return 探访记录
     */
    @Override
    public JSONObject selectElderlyApartmentVisitRecordsById(Long id)
    {
        ElderlyApartmentVisitRecords dto = elderlyApartmentVisitRecordsMapper.selectElderlyApartmentVisitRecordsById(id);
        Elderlyapartment apartmentInfo = null;
        if(Objects.nonNull(dto.getApartmentId())){
            apartmentInfo = elderlyapartmentMapper.getApartmentInfo(dto.getApartmentId());
        }
        ElderlyInfo elderlyInfo = null;
        if(Objects.nonNull(dto.getElderId())){
            elderlyInfo = elderlyInfoMapper.getElderInfoByElderId(dto.getElderId());
        }
        JSONObject obj = new JSONObject();
        obj.put("id", StringNewUtils.long2Str(dto.getId()));
        obj.put("visitorName",dto.getVisitorName());
        obj.put("elderId",StringNewUtils.long2Str(dto.getElderId()));
        obj.put("apartmentId",StringNewUtils.long2Str(dto.getApartmentId()));
        obj.put("visitDate",StringNewUtils.long2Str(dto.getVisitDate()));
        obj.put("apartmentName", Objects.isNull(dto.getApartmentId())?"":apartmentInfo.getApartmentId());
        obj.put("elderName",Objects.isNull(dto.getElderId())?"":elderlyInfo.getElderName());
        obj.put("visitTime",StringNewUtils.long2Str(dto.getVisitTime()));
        obj.put("physicalCondition",dto.getPhysicalCondition());
        obj.put("mentalState",dto.getMentalState());
        obj.put("dietStatus",dto.getDietStatus());
        obj.put("socialInteraction",dto.getSocialInteraction());
        obj.put("specialNeeds",dto.getSpecialNeeds());
        obj.put("notes",dto.getNotes());
        obj.put("suggestions",dto.getSuggestions());
        return obj;
    }

    /**
     * 查询探访记录列表
     *  探访记录
     * @return 探访记录
     */
    @Override
    public TableDataInfo selectElderlyApartmentVisitRecordsList(String visitorName, String elderName, String startTime, String endTime, Integer pageNum, Integer pageSize)
    {
        Long dayStartTime = null;
        Long dayEndTime = null;
        if(!StringNewUtils.isEmpty(startTime) && !StringNewUtils.isEmpty(endTime)) {
            dayStartTime = DateUtils.getDayStartTime(DateUtils.getHMDateSeconds(startTime));
            dayEndTime = DateUtils.getDayEndTime(DateUtils.getHMDateSeconds(endTime));
        }
        PageHelper.startPage(pageNum, pageSize);
        List<ElderlyApartmentVisitRecords> list = elderlyApartmentVisitRecordsMapper.selectElderlyApartmentVisitRecordsList(visitorName,elderName,dayStartTime,dayEndTime);
        PageInfo<ElderlyApartmentVisitRecords> pageInfo = new PageInfo<>(list);
        List<ElderlyApartmentVisitRecords> visitRecords = pageInfo.getList();
        //获取公寓信息
        List<Elderlyapartment> aprtList = elderlyapartmentMapper.selectElderlyapartmentList(new Elderlyapartment());
        //获取探访记录中的居民Ids
        List<Long> elderIds = visitRecords.stream().map(ElderlyApartmentVisitRecords::getElderId).distinct().collect(Collectors.toList());
        if(CollectionUtils.isEmpty(elderIds)){
            return new TableDataInfo().getDataTable(new ArrayList<>());
        }
        Map<Long, String> elderMap = elderlyInfoMapper.getElderInfoByElderIds(elderIds).stream().collect(Collectors.toMap(ElderlyInfo::getElderId, ElderlyInfo::getElderName));
        Map<Long, String> aprtMap = aprtList.stream().collect(Collectors.toMap(Elderlyapartment::getApartmentId, Elderlyapartment::getApartmentName));
        JSONObject obj = null;
        JSONArray result = new JSONArray();
        for(ElderlyApartmentVisitRecords dto:visitRecords){
            obj = new JSONObject();
            obj.put("id", StringNewUtils.long2Str(dto.getId()));
            obj.put("visitorName",dto.getVisitorName());
            obj.put("elderId",StringNewUtils.long2Str(dto.getElderId()));
            obj.put("apartmentId",StringNewUtils.long2Str(dto.getApartmentId()));
            obj.put("visitDate",StringNewUtils.long2Str(dto.getVisitDate()));
            obj.put("apartmentName", Objects.isNull(dto.getApartmentId())?"":aprtMap.get(dto.getApartmentId()));
            obj.put("elderName",Objects.isNull(dto.getElderId())?"":elderMap.get(dto.getElderId()));
            obj.put("visitTime",StringNewUtils.long2Str(dto.getVisitTime()));
            obj.put("physicalCondition",dto.getPhysicalCondition());
            obj.put("mentalState",dto.getMentalState());
            obj.put("dietStatus",dto.getDietStatus());
            obj.put("socialInteraction",dto.getSocialInteraction());
            obj.put("specialNeeds",dto.getSpecialNeeds());
            obj.put("notes",dto.getNotes());
            obj.put("suggestions",dto.getSuggestions());
            result.add(obj);
        }
        TableDataInfo dataInfo = new TableDataInfo();
        dataInfo.setTotal(pageInfo.getTotal());
        dataInfo.setRows(result);
        dataInfo.setCode(200);
        dataInfo.setMsg("success");
        return dataInfo;
    }

    /**
     * 新增探访记录
     * 
     * @param elderlyApartmentVisitRecords 探访记录
     * @return 结果
     */
    @Override
    public int insertElderlyApartmentVisitRecords(ElderlyApartmentVisitRecords elderlyApartmentVisitRecords)
    {
        elderlyApartmentVisitRecords.setId(SnowGenerator.getId());
        elderlyApartmentVisitRecords.setCreateTime(DateUtils.getLongTime());
        return elderlyApartmentVisitRecordsMapper.insertElderlyApartmentVisitRecords(elderlyApartmentVisitRecords);
    }

    /**
     * 修改探访记录
     * 
     * @param elderlyApartmentVisitRecords 探访记录
     * @return 结果
     */
    @Override
    public int updateElderlyApartmentVisitRecords(ElderlyApartmentVisitRecords elderlyApartmentVisitRecords)
    {
        elderlyApartmentVisitRecords.setUpdateTime(DateUtils.getLongTime());
        return elderlyApartmentVisitRecordsMapper.updateElderlyApartmentVisitRecords(elderlyApartmentVisitRecords);
    }

    /**
     * 批量删除探访记录
     * 
     * @param ids 需要删除的探访记录主键
     * @return 结果
     */
    @Override
    public int deleteElderlyApartmentVisitRecordsByIds(Long[] ids)
    {
        return elderlyApartmentVisitRecordsMapper.deleteElderlyApartmentVisitRecordsByIds(ids);
    }

    /**
     * 删除探访记录信息
     * 
     * @param id 探访记录主键
     * @return 结果
     */
    @Override
    public int deleteElderlyApartmentVisitRecordsById(Long id)
    {
        return elderlyApartmentVisitRecordsMapper.deleteElderlyApartmentVisitRecordsById(id);
    }

    @Override
    public List<ElderlyApartmentVisitRecords> selectElderlyApartmentVisitRecordsExportList(ElderlyApartmentVisitRecords elderlyApartmentVisitRecords) {

        return elderlyApartmentVisitRecordsMapper.selectApartmentVisitRecordsList(elderlyApartmentVisitRecords);
    }
}
